package com.amqr.ammvvm;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Toast;

import com.amqr.ammvvm.adapter.RvMmAdapter;
import com.amqr.ammvvm.bean.UserBean;
import com.amqr.ammvvm.databinding.ActivityRvBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * User: LJM
 * Date&Time: 2017-12-18 & 16:26
 * Describe: Describe Text
 */
public class RvActivity extends AppCompatActivity {

    ActivityRvBinding mActivityRvBinding;
    private List<UserBean> mUserBeanList;
    private final static int CIR= 0X2001;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what){
                case CIR:

                    UserBean userBean1 = mUserBeanList.get(0);
                    userBean1.skill.set("V"+userBean1.skill.get());
                    mUserBeanList.set(0,userBean1);
                    break;
            }

        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivityRvBinding = DataBindingUtil.setContentView(this,R.layout.activity_rv);
        mUserBeanList = initDate();
        mActivityRvBinding.mRv.setLayoutManager(new LinearLayoutManager(this));
        RvMmAdapter rvMmAdapter = new RvMmAdapter(mUserBeanList);
        rvMmAdapter.setChangeClickListener(new RvMmAdapter.IChanheClickListener() {
            @Override
            public void changeClickListener(int position, UserBean userBean) {
                UserBean userBean1 = mUserBeanList.get(position);
                userBean1.skill.set("A"+userBean1.skill.get());
                mUserBeanList.set(position,userBean1);

                Toast.makeText(RvActivity.this,"局部刷新"+position+userBean.name.get(),Toast.LENGTH_SHORT).show();
            }
        });

        //mActivityRvBinding.mRv.setAdapter(rvMmAdapter);
        mActivityRvBinding.setAdapter(rvMmAdapter);

        if(mUserBeanList!=null){

            mActivityRvBinding.mTvCl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    startTimer();
                }
            });

        }

    }

    private List<UserBean> initDate(){
        List<UserBean> data = new ArrayList<>();
        for (int i=0;i<20;i++){
            UserBean userBean = new UserBean();
            userBean.name.set("猴子"+i);
            userBean.skill.set(i+"般变化");
            String s = userBean.skill.get();
            data.add(userBean);
        }
        return data;
    }
    private MyCountDownTimer timer;
    private final long TIME = 10 * 1000L;
    private final long INTERVAL = 1000L;

    public class MyCountDownTimer extends CountDownTimer {
        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            long time = millisUntilFinished / 1000;

            handler.sendEmptyMessage(CIR);
        }

        @Override
        public void onFinish() {
            if (timer != null) {
                timer.cancel();
                timer = null;
            }
        }
    }

    private void startTimer() {
        if (timer == null) {
            timer = new MyCountDownTimer(TIME, INTERVAL);
        }
        timer.start();
    }

}
